/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
/* $Id: tcpserver_epoll.c 16 2007-06-10 02:10:04Z kf701.ye $ */

#include <sys/time.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "kf701.h"

/**
 * @brief TCP server use epoll
 *
 * Open a listen socket,accept and read data,
 * then deleve data to FUNC, the user defined 
 * FUNC deal with data.
 * 
 * Note: dup buf before create thread in FUNC
 *
 * @param port listen port
 * @param psize the TCP server will read MAX 
 * size data from a new accepted socket
 * @param func user defined FUNC for deal with data
 *
 * @return void
 */
void tcp_server_epoll( uint16_t port, uint32_t psize, tcp_data_func func)
{
	if( psize <= 0 || func == NULL )
	{
		sys_message("%s,%d: argu err\n", __FILE__, __LINE__);
		return ;
	}
	
	int sockfd = open_listenfd( port );
	if( -1 == sockfd )
	{
		sys_message("%s,%d: open sock err,%m\n", __FILE__, __LINE__);
		return ;
	}
	
	/* serve C > 10K ,so set resource limit */
	struct rlimit rl = { 30000 , 30000 };
	if ( setrlimit(RLIMIT_NOFILE, &rl) == -1 )
		sys_message("%s,%d: setrlimit err,%m\n", __FILE__, __LINE__);
	
	signal(SIGPIPE,SIG_IGN);

	int size = 30000, epfd, nfds, n, client, fd;
	
	struct epoll_event ev, events[30007];
	epfd = epoll_create(size);
	ev.events = EPOLLIN;
	ev.data.fd = sockfd;
	if( epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev) < 0 )
	{
		sys_message("%s,%d: insert epoll fd err,%m", __FILE__, __LINE__);
		return ;
	}
	
	struct sockaddr_in  addr;
	socklen_t sock_len = sizeof( struct sockaddr_in );
	uint8_t buf[ psize ];
	while(1)
	{
		nfds = epoll_wait(epfd, events, size, -1);
		if(nfds <= 0 )
			continue;

		for(n = 0; n < nfds; n++)
		{
			if( events[n].data.fd == sockfd )
			{
				client = accept( sockfd, (struct sockaddr*)&addr, &sock_len );
				if( client < 0 )
				{
					sys_message("%s,%d: accept err,%m", __FILE__, __LINE__);
					continue;
				}
				sys_log("%s,%d: connect from %s\n",__FILE__,__LINE__,inet_ntoa(addr.sin_addr));
				setnonblocking( client );
				ev.events = EPOLLIN ;
				ev.data.fd = client;
				if (epoll_ctl(epfd, EPOLL_CTL_ADD, client, &ev) < 0)
					sys_message("%s,%d: insert epoll fd err,%m", __FILE__, __LINE__);
				continue;
			}
			fd = events[n].data.fd;
			int data_len = rio_read( fd, buf, sizeof(buf) );

			if( data_len <= 0 )
			{
				epoll_ctl( epfd , EPOLL_CTL_DEL , fd , events );
				close( fd );
				sys_message("%s,%d: read err,close the socket!\n", __FILE__, __LINE__);
				continue;
			}
			sys_log("%s,%d: read data len = %d\n", __FILE__, __LINE__, data_len );
			/* Note: dup buf before create thread in func */
			func( fd, buf, data_len );
		}
	}
}
